package com.tkjt.pms.modules.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tkjt.pms.common.entity.*;
import com.tkjt.pms.modules.common.mapper.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tkjt.pms.modules.common.service.TkIndexValueAuditService;
import com.tkjt.pms.modules.kpiAudit.mapper.KpiAuditMapper;

import lombok.extern.slf4j.Slf4j;

@Service("tkIndexValueAuditService")
@Slf4j
@Transactional
public class TkIndexValueAuditServiceImpl extends ServiceImpl<TkIndexValueAuditMapper, TkIndexValueAudit> implements TkIndexValueAuditService {
    @Autowired
    TkIndexValueMirrorMapper tkIndexValueMirrorMapper;
    @Autowired
    TkIndexValueMapper tkIndexValueMapper;
    @Autowired
    TkIndexValueCommentsMirrorMapper tkIndexValueCommentsMirrorMapper;
    @Autowired
    TkIndexValueCommentsMapper tkIndexValueCommentsMapper;
    @Autowired
    KpiAuditMapper kpiAuditMapper;
    
    /**
     * 审核成功后
     * 生成镜像数据
     */
    @Override
    public void batchDeleteAndSaveEntities(String mirrorCode,String indexGroup,String dataType) {
    	
    	log.info("index value mirror, start...");
    	
        QueryWrapper<TkIndexValueMirror> qw = new QueryWrapper<>();
        qw.eq("MIRROR_CODE",mirrorCode);
        qw.eq("DATA_TYPE",dataType);
        qw.likeRight("INDEX_DEPART",indexGroup);
        log.info("clear mirror data, start...");
        tkIndexValueMirrorMapper.delete(qw);
        log.info("clear mirror data, end.");

        log.info("indexValue, start...");
        QueryWrapper<TkIndexValue> indexVlueQuery = new QueryWrapper<>();
        indexVlueQuery.eq("DATA_TYPE",dataType);
        indexVlueQuery.likeRight("INDEX_DEPART",indexGroup);
        List<TkIndexValue> indexValueList = tkIndexValueMapper.selectList(indexVlueQuery);
        List<TkIndexValueMirror> mirrorList = new ArrayList<>();
        for(TkIndexValue item : indexValueList){
            TkIndexValueMirror newMirror = new TkIndexValueMirror();
            BeanUtils.copyProperties(item,newMirror);
            //数据镜像标识:MC+六位年月,如:MC201911
            newMirror.setMirrorCode(mirrorCode);
            mirrorList.add(newMirror);
        }
        log.info("indexValue mirror size={}", mirrorList.size());
        if(mirrorList.size() > 0){
            for(TkIndexValueMirror tkIndexValueMirror : mirrorList){
                tkIndexValueMirrorMapper.insert(tkIndexValueMirror);
            }
        }
        log.info("indexValue, end.");

        log.info("indexComments, start...");
        //评分意见镜像表
        Map<String,Object> mirroParam = new HashMap<>();
        mirroParam.put("dataType",dataType);
        mirroParam.put("mirrorCode",mirrorCode);
        mirroParam.put("indexGroup",indexGroup);
        kpiAuditMapper.deleteCommentsMirroListByParam(mirroParam);
        Map<String,Object> commentsParam = new HashMap<>();
        commentsParam.put("dataType",dataType);
        commentsParam.put("indexGroup",indexGroup);
        List<TkIndexValueComments> commentsDatas =kpiAuditMapper.selectCommentsListByDataType(commentsParam);
        if(null == commentsDatas) {
            return;
        }
        List<TkIndexValueCommentsMirror> commentsMirrorList = new ArrayList<>();
        for(TkIndexValueComments item : commentsDatas){
            TkIndexValueCommentsMirror newMirror = new TkIndexValueCommentsMirror();
            BeanUtils.copyProperties(item,newMirror);
            //数据镜像标识:MC+六位年月,如:MC201911
            newMirror.setMirrorCode(mirrorCode);
            commentsMirrorList.add(newMirror);
        }
        log.info("indexComments mirror size={}", commentsMirrorList.size());
        if(commentsMirrorList.size() > 0){
            for(TkIndexValueCommentsMirror tkIndexValueCommentsMirror : commentsMirrorList){
                tkIndexValueCommentsMirrorMapper.insert(tkIndexValueCommentsMirror);
            }

        }
        log.info("indexComments, end.");

        log.info("index value mirror, end.");


    }
}
